<?php

require_once 'modules/product/models/Product.php';
require_once 'modules/product/models/ProductCategory.php';
require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';

class product_IndexController extends Nine_Controller_Action
{
	
	public function indexAction()
	{
		$objProduct = new Models_Product();
		$objProductCategory = new Models_ProductCategory();
		$objContent = new Models_Content();
		$objCatCon = new Models_ContentCategory();
		/**
		 * Get category
		 */
		$categoryGid = $this->_getParam('cid',false);
		if (false == $categoryGid) {
			$this->_redirectToNotFoundPage();
		}
		$allcat = $objProductCategory->getAllCategories();
		
		foreach ($allcat as &$item){
        	$item['url'] = Nine_Route::_("product/index/index/cid/".$item['product_category_gid'],array('alias' => $item['alias']));
        }
        unset($item);
		$cat = $objProductCategory->getCategoryWithParent($categoryGid);
		if (null == $cat['product_category_id']) {
			$this->_redirectToNotFoundPage();
		}
		
		
		/**
         * Get number row per page
         */
		
		$numRowPerPage = Nine_Registry::getConfig("defaultNumberRowPerPage");
		
		$currentPage = $this->_getParam("page",1);

		/**
		 * Get all Products
		 */
		$allProducts = $objProduct->getAllEnabledProductsByCategory($categoryGid, array('sorting ASC','product_id DESC'),
                                                   $numRowPerPage,($currentPage - 1) * $numRowPerPage
                                                   );
//        echo "<pre>";print_r($allProducts);die; 
        /**
         * Modified all products
         */
                                                   
        foreach($allProducts as &$product){
        	$product['created_date'] = date(Nine_Registry::getConfig('dateFormat'),$product['created_date']);
        	if (null != $product['sale_off_start_date']) {
        		$product['sale_off_start_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_start_date']);
        	}
        	if (null != $product['sale_off_end_date']){
        		$product['sale_off_end_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_end_date']);
        	}

        	$product['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($product['title'])),30);
        	$product['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($product['intro_text'])), 145);
        	$product['link'] = Nine_Route::_("product/index/detail/id/{$product['product_gid']}",array('alias' => $product['alias']));
        	
        	if (null != $product['images']) {
        		$product['images'] = explode("||", $product['images']);
        		$product['main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 230,230,null,true);
        		$product['full_main_image'] = Nine_Function::getThumbImage(@$product['images'][0],  500 ,500,null,true);
        	}
        	else {
        			$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noimage.jpg",218,136);
        	}
        	$product['price'] = $objProduct->makeUpPrice($product['price']);
        
        }
        unset($product);
        
        /**
         * Count all products
         */
        $count = count($objProduct->getAllEnabledProductsByCategory($categoryGid));
        /**
         * Assign to rendered view
         */
        $this->setPagination($numRowPerPage, $currentPage, $count);
        $this->view->allProducts = $allProducts;
        
        $rootCatParent = $objProductCategory->getRootParent($categoryGid);
        $this->view->allCat = $allcat;
        $this->view->menuId = "shop";
        $this->view->idMenu = $categoryGid;
	}
public function allAction()
	{
		$objProduct = new Models_Product();
		$objProductCategory = new Models_ProductCategory();
		$objContent = new Models_Content();
		$objCatCon = new Models_ContentCategory();
		/**
		 * Get category
		 */
		$categoryGid = $this->_getParam('cid',false);
		
		if (false == $categoryGid) {
			$this->_redirectToNotFoundPage();
		}
		
		$cat = $objProductCategory->getCategoryWithParent($categoryGid);
		if (null == $cat['product_category_id']) {
			$this->_redirectToNotFoundPage();
		}
	if($cat['parent_id'] == 3 || $cat['product_category_gid'] == 3){
			$this->setLayout('ao');
		}
		/**
         * Get number row per page
         */
		
		$numRowPerPage = Nine_Registry::getConfig("defaultNumberRowPerPage");
		
		$currentPage = $this->_getParam("page",1);

		/**
		 * Get all Products
		 */
		$allProducts = $objProduct->getAllEnabledProductsByCategory($categoryGid, array('sorting ASC','product_id DESC'),
                                                   $numRowPerPage,($currentPage - 1) * $numRowPerPage
                                                   );
//        echo "<pre>";print_r($allProducts);die; 
        /**
         * Modified all products
         */
                                                   
        foreach($allProducts as &$product){
        	$product['created_date'] = date(Nine_Registry::getConfig('dateFormat'),$product['created_date']);
        	if (null != $product['sale_off_start_date']) {
        		$product['sale_off_start_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_start_date']);
        	}
        	if (null != $product['sale_off_end_date']){
        		$product['sale_off_end_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_end_date']);
        	}

        	$product['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($product['title'])),30);
        	$product['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($product['intro_text'])), 145);
        	$product['link'] = Nine_Route::_("product/index/detail/id/{$product['product_gid']}",array('alias' => $product['alias']));
        	
        	if (null != $product['images']) {
        		$product['images'] = explode("||", $product['images']);
        			$product['main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 255,152,null,true);
        			$product['full_main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 800);
        	}
        	else {
        			$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noimage.jpg",218,136);
        	}
        	$product['price'] = $objProduct->makeUpPrice($product['price']);
        }
        unset($product);
        
        /**
         * Count all products
         */
        $count = count($objProduct->getAllEnabledProductsByCategory($categoryGid));
        
        
        $condition['product_category_gid'] = $categoryGid;
        	$allNews = $objContent->getAllEnabledContentByCategory(62,$condition,'content_id DESC',6);
			
        	foreach ($allNews as &$item){
				if(null != $item['images']){
				$tmp = explode('||', $item['images']);
				$item['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 239);
				}else{
				$item['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noImage.jpg", 150);
				}	
				$item['link'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
				if(null != $item['intro_text']){
					$item['intro_text'] = $item['intro_text'];
				}else{
					$item['intro_text'] = Nine_Function::subStringAtBlank($item['full_text'],300);
				}
			}
			unset($item);
        /**
         * Count all products
         */
        $count = count($objProduct->getAllEnabledProductsByCategory($categoryGid));
        
        
		
		$this->view->allNews = $allNews;
		
		$news = $objCatCon->getCategoryWithParent(62);
		$param = array();
		$param['alias'] = $news['alias'];
		$newsURL = Nine_Route::_('content/index/news' , $param);
		$this->view->newsURL = $newsURL;
        
        
        /**
         * Assign to rendered view
         */
        $this->view->headTitle($cat['name']);
        $this->setPagination($numRowPerPage, $currentPage, $count);
       	$this->view->cat = $cat;
        $this->view->allProducts = $allProducts;
        
        $rootCatParent = $objProductCategory->getRootParent($categoryGid);
        $this->view->menuId = "product_category_{$rootCatParent['product_category_gid']}";
	}
	
	
	public function detailAction() {
		$objProduct = new Models_Product();
		$objProductCategory = new Models_ProductCategory();
		
		$gid = $this->_getParam('id',false);
		if (false == $gid) {
			$this->_redirectToNotFoundPage();
		}
		
		$product = $objProduct->getProductById( $gid );
		if (null == $product) {
			$this->_redirectToNotFoundPage();
		}
		/**
		 * Modified product
		 */
		$product['created_date'] = date(Nine_Registry::getConfig('dateFormat'),$product['created_date']);
        if (null != $product['sale_off_start_date']) {
        	$product['sale_off_start_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_start_date']);
        }
        if (null != $product['sale_off_end_date']){
        	$product['sale_off_end_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_end_date']);
        }
        
        $cat = @reset($objProductCategory->getByColumns(array('product_category_gid=?'=>$product['product_category_gid']))->toArray());
		$product['category_name'] = $cat['name'];
		if ( null != @$product['images']) {
			$product['images'] = explode("||", $product['images']);
			$product['main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 458);
			$product['full_main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 800,null,null,true);

			/**
			 * Get more thumbnail images
			 */
			foreach ($product['images'] as &$image) {
			    $image = array(
			    	'thumbnail' => Nine_Function::getThumbImage($image, 102, 80),
			    	'full_image' => Nine_Function::getThumbImage($image, 800)
			    );
			}
			unset($image);

		}
        else {
        	$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noimage.jpg",458);
        }
		
        if (null != @$product['price']) {
        	$product['price'] = $objProduct->makeUpPrice($product['price']);
        }
        
        
		if (null != @$news['meta_keywords']) {
        	$this->view->headMetaKeywords($news['meta_keywords']);
        }
        
	 	if (null != @$news['meta_description']) {
        	$this->view->headMetaDescription($news['meta_description']);
        }
        /**
         * Assign to rendered view
         */
        $product['attributes'] = explode(',', $product['attributes']);
        $this->view->headTitle($product['title']);
        $this->view->product = $product;
         $this->view->randomNumber = rand(0, 1000000);
        $rootCatParent = $objProductCategory->getRootParent($product['product_category_gid']);
        
        $this->view->menuId = "product_category_{$rootCatParent['product_category_gid']}";
        $arr = $this->session->contactMessage;
		$this->view->mess = $arr['message'];
		unset($this->session->contactMessage);
        /**
		 * Render this template
		 */
	}
	
	
	private function _redirectToNotFoundPage()
	{
	    $this->_redirect("");
	}
	
}